package modelos;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;




public class MConexion{
    protected Connection conexion;
    
    public void conectar(){
        try {
            conexion = DriverManager.getConnection("jdbc:postgresql://localhost:5432/BD6-3", "postgres", "123456");
       } catch (SQLException ex) {
            Logger.getLogger(MConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void desconectar(){
        try {
            conexion.close();            
        } catch (SQLException ex) {
            Logger.getLogger(MConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     public final int generarCodigo(String tabla){
        conectar();
        int cantidad = -1;
        try {
            Statement stam = conexion.createStatement();
            ResultSet rs = stam.executeQuery("select count(*) from "+tabla);
            if(rs.next())
                cantidad = rs.getInt(1);
        } catch (SQLException ex) {
            Logger.getLogger(MConexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        desconectar();
        return cantidad+1;
    }


    public Connection getConexion() {
        return conexion;
    }



    
}
